package jdbc;

import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * 获取数据库连接的5种方式
 */
public class JdbcConn {

    public static void main(String[] args) throws Exception {

        /*
            获取数据库连接的5种方式：
            1.获取Driver实现类对象，静态加载，灵活性差
            2.使用反射获取，动态加载，更加灵活，减少依赖性
            3.使用 DriverManager 替代 Driver 进行统一管理
            4.使用 DriverManager 替代 Driver 进行统一管理，可以省略注册驱动
            5.将连接信息存入properties文件中，读取文件获取信息并使用DriverManager获取连接
         */
    }

    @Test
    public void connect01() throws Exception {
        //1.获取Driver实现类对象，静态加载，灵活性差
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名密码放入到Properties对象中
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lmy");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    @Test
    public void connect02() throws Exception {
        //1.使用反射获取，动态加载，更加灵活，减少依赖性
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名密码放入到Properties对象中
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lmy");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

    @Test
    public void connect03() throws Exception {
        //1.使用 DriverManager 替代 Driver 进行统一管理
        //使用反射加载Driver
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名密码放入到Properties对象中
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lmy");
        //注册driver驱动
        DriverManager.registerDriver(driver);
        //通过DriverManager获取连接
        Connection connection = DriverManager.getConnection(url, properties);
        System.out.println(connection);
    }

    @Test
    public void connect04() throws Exception {
        // class.forName自动完成驱动，简化代码
        //使用反射加载Driver
        /*
            1.静态代码块在类加载时会执行一次
            2.DriverManager.registerDriver(new Driver());
            3.因此注册driver工作已经完成
            static {
                    try {
                        DriverManager.registerDriver(new Driver());
                     } catch (SQLException var1) {
                        throw new RuntimeException("Can't register driver!");
                     }
            }

         */
        /*
            其实可以省略加载driver类的操作
            从jdk1.5以后使用了jdbc4，不再需要显示调用class.forName()注册驱动而是自动调用驱动
            jar包下META-INF\services\java.sql.Driver文本中的类名称去注册
         */
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名密码放入到Properties对象中
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "lmy");
        Connection connect = DriverManager.getConnection(url, properties);
        System.out.println(connect);
    }

    @Test
    public void connect05() throws Exception {

        //通过Properties 对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql3.properties"));
        //获取相关值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
//        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

}
